package com.friend.joey.applicationfriend.Fragments;


import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.friend.joey.applicationfriend.Adapters.MsgAdapter;
import com.friend.joey.applicationfriend.ChildActivities.ChildForgetActivity;
import com.friend.joey.applicationfriend.ChildActivities.ChildTalkActivity;
import com.friend.joey.applicationfriend.Classes.Msgtalk;
import com.friend.joey.applicationfriend.Classes.RememRecord;
import com.friend.joey.applicationfriend.Database.MyDatabaseHelper;
import com.friend.joey.applicationfriend.MainActivity;
import com.friend.joey.applicationfriend.R;

import java.util.ArrayList;

import static android.R.attr.id;
import static android.R.attr.text;


/**
 * Created by Joey on 2017/4/20.
 */

public class FragmentHealth extends Fragment {
    private MainActivity mactivity;
    private Toolbar toolbar;
    private RecyclerView msgRecyclerView;
    private ImageButton button_send;
    private TextView textInput;
    private MsgAdapter adapter;
    private ArrayList<Msgtalk> msgList = new ArrayList<>();//聊天消息

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_health, container, false);
        mactivity =(MainActivity)getActivity();
        toolbar=(Toolbar)view.findViewById(R.id.toolbarHealth);//
        msgRecyclerView=(RecyclerView)view.findViewById(R.id.fhmsg_recycler_view);
        button_send=(ImageButton)view.findViewById(R.id.button_fhsend);
        textInput=(TextView)view.findViewById(R.id.text_fhinput);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //
        setHasOptionsMenu(true);
        if (toolbar != null) {
            toolbar.setTitle("  ");
            toolbar.setNavigationIcon(R.drawable.computer3);
        }
        mactivity.setSupportActionBar(toolbar);
        //
        initMsgs2();
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        msgRecyclerView.setLayoutManager(layoutManager);
        adapter = new MsgAdapter(msgList,MsgAdapter.CHILD);
        msgRecyclerView.setAdapter(adapter);
        msgRecyclerView.scrollToPosition(msgList.size()-1);//定位到最后一行

        button_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String sendtxt=textInput.getText().toString();
                //更新对话框显示
                if(!"".equals(sendtxt)) {

                    Msgtalk smsg = new Msgtalk(sendtxt, Msgtalk.TYPE_OLDMAN);
                    msgList.add(smsg);
                    adapter.notifyItemChanged(msgList.size()-1);//有新消息时刷新显示
                    msgRecyclerView.scrollToPosition(msgList.size()-1);//定位到最后一行
                    textInput.setText("");//清空输入框的内容
                    //添加进数据库
                    mactivity.insertIntoFamilyDatabase(smsg);
                }
                else{
                    Toast.makeText(mactivity, "空信息无法发送", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initMsgs(){
        Msgtalk msg1= new Msgtalk("吃饭了吗",Msgtalk.TYPE_CHILD);
        msgList.add(msg1);
        Msgtalk msg2= new Msgtalk("吃过了",Msgtalk.TYPE_OLDMAN);
        msgList.add(msg2);
    }
    private void initMsgs2(){
        Msgtalk rem0;
        MyDatabaseHelper dbHelper = new MyDatabaseHelper(mactivity,"TestTalk.db",null,2);//数据库
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursor = db.query("talkchild",null,null,null,null,null,null);
        if(cursor.moveToFirst()){
            while(cursor.moveToNext())
            {
                String text= cursor.getString(cursor.getColumnIndex("txt"));
                int side=cursor.getInt(cursor.getColumnIndex("side"));
                rem0=new Msgtalk(text,side);
                msgList.add(rem0);
            }
        }
        else{
            Msgtalk msg1= new Msgtalk("吃饭了吗",Msgtalk.TYPE_CHILD);
            msgList.add(msg1);
            Msgtalk msg2= new Msgtalk("吃过了",Msgtalk.TYPE_OLDMAN);
            msgList.add(msg2);
            Toast.makeText(mactivity, "数据库未获取到信息", Toast.LENGTH_SHORT).show();
        }
    }
    public void setTextInput(String result){
        textInput.setText(result);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                //Toast.makeText(mactivity,"侧边栏",Toast.LENGTH_SHORT).show();
                mactivity.showDrawer();
                break;
        }
        return true;
    }

    public static FragmentHealth newInstance(String arg){
        FragmentHealth FragmentHealth = new FragmentHealth();
        Bundle args = new Bundle();
        args.putString("fHealth", arg);
        FragmentHealth.setArguments(args);
        return FragmentHealth;
    }
}
